package com.example.demotest;

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class Snippet {
	/**
	     * 
	     * 【功能描述：添加图片和文字水印】 【功能详细描述：功能详细描述】
	     * @param srcFile 待加水印文件
	     * @param destFile 加水印后存放地址
	     * @param text 加水印的文本内容
	     * @param textWidth 文字横坐标
	     * @param textHeight 文字纵坐标
	     * @throws Exception
	     */
	    public static void addWaterMark(String srcFile, String destFile, String text,
	            Integer textWidth, Integer textHeight) throws Exception
	    {
	        // 待加水印的文件
	        PdfReader reader = new PdfReader(srcFile);
	        // 加完水印的文件
	        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
	                destFile));
	        int total = reader.getNumberOfPages() + 1;
	        PdfContentByte content;
	        // 设置字体
	        BaseFont font = BaseFont.createFont();
	        // 循环对每页插入水印
	        for (int i = 1; i < total; i++)
	        {
	            // 水印的起始
	            content = stamper.getUnderContent(i);
	            // 开始
	            content.beginText();
	            // 设置颜色 默认为蓝色
	            content.setColorFill(Color.BLUE);
	            // content.setColorFill(Color.GRAY);
	            // 设置字体及字号
	            content.setFontAndSize(font, 38);
	            // 设置起始位置
	            // content.setTextMatrix(400, 880);
	            content.setTextMatrix(textWidth, textHeight);
	            // 开始写入水印
	            content.showTextAligned(Element.ALIGN_LEFT, text, textWidth,
	                    textHeight, 45);
	            content.endText();
	        }
	        stamper.close();
	    }
	    
	    public static void waterMark(String inputFile, String imageFile,
				String outputFile, String waterMarkName, int permission) {
			try {
				PdfReader reader = new PdfReader(inputFile);
				PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
						outputFile));
	 
				BaseFont base = BaseFont.createFont(
						"C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体
	 
				int total = reader.getNumberOfPages() + 1;
				/*Image image = Image.getInstance(imageFile);
	 
				// 图片位置
				image.setAbsolutePosition(400, 480);*/
				PdfContentByte under;
				int j = waterMarkName.length();
				char c = 0;
				int rise = 0;
				for (int i = 1; i < total; i++) {
					rise = 400;
					under = stamper.getUnderContent(i);
					under.beginText();
					under.setFontAndSize(base, 140);
					PdfGState gs = new PdfGState();
					gs.setFillOpacity(0.3f);// 设置透明度为0.8
					under.setGState(gs);
					under.setTextMatrix(20, -175);
					for (int k = 0; k < j; k++) {
						under.setTextRise(rise);
						c = waterMarkName.charAt(k);
						under.showText(c + "");
						rise += 90;
					}
					/*if (j >= 15) {
						under.setTextMatrix(200, 120);
						for (int k = 0; k < j; k++) {
							under.setTextRise(rise);
							c = waterMarkName.charAt(k);
							under.showText(c + "");
						}
					} else {
						under.setTextMatrix(240, 50);
						for (int k = 0; k < j; k++) {
							under.setTextRise(rise);
							c = waterMarkName.charAt(k);
							under.showText(c + "");
							rise -= 18;
	 
						}
					}*/
					
					// under.setTextRise(45);
					// 添加水印文字
					under.endText();
	 
					// 添加水印图片
					// under.addImage(image);
	 
					// 画个圈
					// under.ellipse(250, 450, 350, 550);
					under.setLineWidth(1f);
					under.stroke();
				}
				stamper.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	    
	    public static void main(String[] args) {
	    	// D:/Develop/HelloWorld.pdf
	    	String srcFile = "D:/Develop/上海昊沧保养报告 (6).pdf";
	    	String destFile = "D:/Develop/上海昊沧保养报告 (6)"+System.currentTimeMillis()+".pdf";
	    	try {
				// addWaterMark(srcFile, destFile, "上海昊沧系统控制技术有限责任公司", 500, 50);
	    		waterMark(srcFile, null, destFile, "上海昊沧", 16);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
}

